package com.qf.entity;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor{


    //调用handler之前有机会拦截!即在执行controller中的方法之前。可用于设置编码格式，md5加密，权限管理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("request = [" + request + "], response = [" + response + "], handler = [" + handler + "]");
        return true;//在验证用户是否登录很有效，即当request.getSession（""）获取session中存的值，为null设置为false，并且跳转到登录页面//只有设置了true，下面的两个方法才能执行。false代表拦截，true代表放行。设置完false也可以使用转发或者重定向
    }


    //当执行完handler以后执行，即执行完controller对应的方法之后执行下面这个方法
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("posthandler执行");
    }

    //当试图渲染完毕! 试图解析器找到了对应的页面之后执行下面行代码。即到了对应页面执行这行代码
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
          System.out.println("afterCompletion执行");
    }
}
